﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;

namespace IronOne.SalesOne.SalesOneWeb.EventLogging
{
    public partial class EventAuditor : SOSecurityPage, IntTest, IDataPage
    {
        AuditFacade facade;
        private int pageNo = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidateUserCredentials(Request.Url.LocalPath);

            if(!Page.IsPostBack)
                UCGrid1.SetText("1");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            UCGrid1.SetText("1");

            if (ddlAction.SelectedIndex > 0 && ddlModules.SelectedIndex > 0)
                gridDataBind(pageNo); 
            else
                MassageSummery("IIS501");
        }

        private void gridDataBind(int pageNo)
        {
            facade = new AuditFacade();

            int? module = null;
            DateTime? from = null, to = null;
            string action = null,userName = null;
            if (!string.IsNullOrEmpty(txtEffectiveFrom.Text))
                from = DateTime.Parse(txtEffectiveFrom.Text);
            if (!string.IsNullOrEmpty(txtEffectiveTo.Text))
                to = DateTime.Parse(txtEffectiveTo.Text);
            if (ddlModules.SelectedIndex > 0)
                module = int.Parse(ddlModules.SelectedValue);
            if(ddlAction.SelectedIndex > 0)
                action = ddlAction.SelectedValue;
            if(!string.IsNullOrEmpty(txtUserName.Text))
                userName = txtUserName.Text;

            List<EventLog> list = facade.GetEventLogs(module, from, to, action, userName, pageNo);
            if (list.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
            {
                UCGrid1.IsLastPage = (facade.GetEventLogs(module, from, to, action, userName, pageNo + 1)).Count > 0 ? false : true;
            }


            int[] numbers = { 0, 1, 2, 5};
            UCGrid1.styleList = new List<GridViewStyle> {                
                new GridViewStyle{
                    ColumnId = "Date",                    
                    ColumnWidth = 120},
                new GridViewStyle{
                    ColumnId = "DoneBy",
                    ColumnHeader = "Done By",
                    ColumnWidth = 80},
                new GridViewStyle{
                    ColumnId = "Description",
                    ColumnWidth = 200}
            };

            UCGrid1.SetDataSource(list.Count > 0 ? list : null, numbers);

            if (list.Count <= 0)
            {
                MassageSummery("IIS504");
            }
        }




        #region IntTest Members

        public void Next()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                UCGrid1.SetText(pageNo.ToString());

            }
            else
            {
                pageNo = 2;
                UCGrid1.SetText("2");

            }

            gridDataBind(pageNo);
        }

        public void Previous()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                UCGrid1.SetText(pageNo.ToString());

                gridDataBind(pageNo);
            }
        }

        public void Go()
        {
            int p = int.Parse(UCGrid1.GetText());
            if (p != 0)
            {
                try
                {
                    gridDataBind(p);
                    pageNo = p;
                }
                catch (Exception ex)
                {

                    Response.Write(ex.Message);
                }


            }

        }

        #endregion

        #region IDataPage Members

        public void Create()
        {
            
        }

        public void View()
        {
            
        }

        public void Update()
        {
            
        }

        public void Delete()
        {
            
        }

        public void Cancel()
        {
            
        }

        public void GetId(GridViewRow row)
        {
            
        }

        #endregion
    }
}
